function disableField(arrFldId) {
	for (var i = 0; i < arrFldId.length; i++) {
		var obj = document.getElementById(arrFldId[i]);
		if (obj) {
			obj.disabled = true;
		}
	}
}

function disableFieldName(arrFldName) {
	for (var i = 0; i < arrFldName.length; i++) {
		var arrObj = document.getElementsByName(arrFldName[i]);
		for (var j = 0; j < arrObj.length; j++) {
			var obj = arrObj[j];
			if (obj) {
				obj.disabled = true;
			}
		}
	}
}

function enableField(arrFldId) {
	for (var i = 0; i < arrFldId.length; i++) {
		var obj = document.getElementById(arrFldId[i]);
		if (obj) {
			obj.disabled = false;
		}
	}
}

function enableFieldName(arrFldName) {
	for (var i = 0; i < arrFldName.length; i++) {
		var arrObj = document.getElementsByName(arrFldName[i]);
		for (var j = 0; j < arrObj.length; j++) {
			var obj = arrObj[j];
			if (obj) {
				obj.disabled = false;
			}
		}
	}
}

function toInt(txt) {
	txt = txt.replace(/,/g, "");
	return parseInt(txt);
}

function toFloat(txt) {
	txt = txt.replace(/,/g, "");
	return parseFloat(txt);
}

function addCommas(txt) {
	txt = txt.replace(",", "");
	txt += '';
	x = txt.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function addCommas2D(txt) {
	if(txt != "" && txt != null){
		txt = txt.replace(",", "");
		txt += '';
		x = txt.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + (x[1] + "00").substring(0, 2) : '.00';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}else{
		return "";
	}
}
function addCommasNoReplace2D(txt) {
	if(txt != "" && txt != null){
		txt += '';
		x = txt.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + (x[1] + "00").substring(0, 2) : '.00';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}else{
		return "";
	}
}
function checkCitizenId(id) {
if(id.length != 13) return false;
for(i=0, sum=0; i < 12; i++)
sum += parseFloat(id.charAt(i))*(13-i); if((11-sum%11)%10!=parseFloat(id.charAt(12)))
return false; return true;
}

function accountNoFormat(obj) {
	var acc = obj.value;
	if (acc.length >= 10) {
		obj.value = acc.substring(0, 3) + "-" + acc.substring(3, 4) + "-" + acc.substring(4, 9) + "-" + acc.substring(9, 10);
	}
}

function dateFormat(obj) {
	var date = obj.value;
	var d = new Date();
	var yyyyEn = d.getFullYear();
	var yyyyTh = d.getFullYear() + 543;
	var pattern1 = /^\d{8}$/;
	var patt1 = new RegExp(pattern1);
	var pattern2 = /^\d{6}$/;
	var patt2 = new RegExp(pattern2);
	var pattern3 = /^\d{4}$/;
	var patt3 = new RegExp(pattern3);
	var strDate = "";
	date = date.replace(/(\-|\/|\.)/g, "");
	if (date.length == 4 || date.length == 6 || date.length == 8) {
		if (date.length == 6 ) {
			var yyyy = date.substring(4, date.length);
			if (yyyy.length == 2) {
				var yyTh = new String(yyyyTh);
				yyyyTh = yyTh.substring(0, 2) + yyyy;
			}
		}
		if (patt1.test(date)) {
			strDate = date.substring(0, 2) + "/" + date.substring(2, 4) + "/" + date.substring(4, 8);
		} else if (patt2.test(date) || patt3.test(date)) {
			strDate = date.substring(0, 2) + "/" + date.substring(2, 4) + "/" + yyyyTh;
		}
		if (strDate != "") {
			if (isDate(strDate)) {
				obj.value = strDate;
			} else {
				obj.value = "";
			}
		}
	} else {
		obj.value = "";
	}
}

function isDate(dateStr) {
	var date = dateStr.replace(/(\-|\/|\.)/g, "");
	
	day = new Number(date.substring(0, 2));
	month = new Number(date.substring(2, 4)); // p@rse date into variables
	year = new Number(date.substring(4, 8)) - 543;
	
	var d = new Date();
	var dd = d.getDate();
	var mm = d.getMonth() + 1;
	var yyyy = d.getFullYear();
	
	/*if (year > yyyy) {
		return false;
	}
	
	if (year == yyyy && month > mm) {
		return false;
	}
	
	if (year == yyyy && month == mm && day > dd) {
		return false;
	}*/
	
	if (month < 1 || month > 12) { // check month range
		return false;
	}
	
	if (day < 1 || day > 31) {
		return false;
	}
	
	if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			return false;
		}
	}
	return true;
}

function setAddress(code, arrFldId) {
	var tc = "";
	var tn = "";
	var ac = "";
	var an = "";
	var pc = "";
	var pn = "";
	var post = "";
	var tCode = document.getElementById(arrFldId[0]);
	var tName = document.getElementById(arrFldId[1]);
	var aCode = document.getElementById(arrFldId[2]);
	var aName = document.getElementById(arrFldId[3]);
	var pCode = document.getElementById(arrFldId[4]);
	var pName = document.getElementById(arrFldId[5]);
	var postCode = document.getElementById(arrFldId[6]);
	if (code != "" && code.indexOf(", ") >= 0) {
		var arrCode = code.split(", ");
		var tap = arrCode[0];
		tc = tap.substring(0, 8);
		tn = arrCode[1];
		ac = tap.substring(0, 4);
		an = arrCode[2];
		pc = tap.substring(0, 2);
		pn = arrCode[3];
		post = arrCode[4];
	}
	if (code == "" || code.indexOf(", ") >= 0) {
		if (tCode) {
			tCode.value = tc;
		}
		if (tName) {
			tName.value = tn;
		}
		if (aCode) {
			aCode.value = ac;
		}
		if (aName) {
			aName.value = an;
		}
		if (pCode) {
			pCode.value = pc;
		}
		if (pName) {
			pName.value = pn;
		}
		if (postCode) {
			postCode.value = post;
		}
	}
}

function validateEmpty() {
	var submit = true;
	var require = false;
	var arrSpan = document.getElementsByTagName("span");
	for (var i = 0; i < arrSpan.length; i++) {
		var span = arrSpan[i];
		var txt = "*";
		var obj = null;
		var type = span.id.substring(0, 1); // #id = input[text, textarea], @name = input[radio, checkbox], $id = select
		if (type == "#") {
			obj = document.getElementById(span.id.substring(1));
			if (obj && !obj.disabled && obj.value == "") {
				txt = "กรุณากรอกข้อมูล";
				obj.focus();
				require = true;
			}
			span.innerHTML = txt;
		} else if (type == "@") {
			obj = document.getElementsByName(span.id.substring(1));
			var c = 0;
			if (obj) {
				for (var x = 0; x < obj.length; x++) {
					if (!obj[x].disabled && !obj[x].checked) {
						c++;
					}
				}
			}
			if (c == x) {
				txt = "กรุณาเลือกข้อมูล";
				require = true;
			}
			span.innerHTML = txt;
		} else if (type == "$") {
			obj = document.getElementById(span.id.substring(1));
			if (obj && !obj.disabled && obj.value == "") {
				txt = "กรุณาเลือกข้อมูล";
				if (!document.activeElement) {
					obj.focus();
				}
				require = true;
			}
			span.innerHTML = txt;
		}
		if (require) {
			submit = false;
			break;
		}
	}
	return submit;
}

function validateEmptyById(arrId) {
	var submit = true;
	var require = false;
	for (var i = 0; i < arrId.length; i++) {
		var span = document.getElementById(arrId[i]);
		var txt = "*";
		var obj = null;
		var type = span.id.substring(0, 1); // #id = input[text, textarea], @name = input[radio, checkbox], $id = select
		if (type == "#") {
			obj = document.getElementById(span.id.substring(1));
			if (obj && !obj.disabled && obj.value == "") {
				txt = "กรุณากรอกข้อมูล";
				obj.focus();
				require = true;
			}
			span.innerHTML = txt;
		} else if (type == "@") {
			obj = document.getElementsByName(span.id.substring(1));
			var c = 0;
			if (obj) {
				for (var x = 0; x < obj.length; x++) {
					if (!obj[x].disabled && !obj[x].checked) {
						c++;
					}
				}
			}
			if (c == x) {
				txt = "กรุณาเลือกข้อมูล";
				require = true;
			}
			span.innerHTML = txt;
		} else if (type == "$") {
			obj = document.getElementById(span.id.substring(1));
			if (obj && !obj.disabled && obj.value == "") {
				txt = "กรุณาเลือกข้อมูล";
				obj.focus();
				require = true;
			}
			span.innerHTML = txt;
		}
		if (require) {
			submit = false;
			break;
		}
	}
	return submit;
}

function thaiDatepicker(id,attributesType,mode){
	var strId = attributesType+""+id;
	$(function(){
		$(strId).datepicker({
			dateFormat : 'dd/mm/yy',
			showOn: 'button',
			buttonImage: 'images/calendar_full.png',
			buttonImageOnly: true,
			dayNamesMin: ['อา', 'จ', 'อ', 'พ', 'พฤ', 'ศ', 'ส'], 
			monthNamesShort: ['มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน','กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'],
			changeMonth: true,
			changeYear: true ,
			isBuddhist: true 
		});
	});
}

function diffDate(startDate, endDate,string){
	var result;
	startDate = startDate.split(string);
	endDate = endDate.split(string);
	sDate = new Date(startDate[2],startDate[1]-1,startDate[0]); 
	eDate = new Date(endDate[2],endDate[1]-1,endDate[0]); 
	var daysDiff = Math.round((eDate-sDate)/86400000);
	if(daysDiff >= 0){
		result = true;
	}else{
		result = false;
	}
	return result;
}

function dateDiff(startDate, endDate,string){
	var result;	
	var sDate = "";
	var eDate = "";
	startDate = startDate.split(string);
	endDate = endDate.split(string);
	sDate = startDate[2]+startDate[1]+startDate[0];
	eDate = endDate[2]+endDate[1]+endDate[0];
	if ((parseInt(eDate) - parseInt(sDate)) >= 0) {
		result = true;
	} else {
		result = false;
	}
	return result;
}

function changeDateTh2En(date){
	date = date.split("/");
	date = (date[2]-543) + date[1] + date[0];
	return date;
}

function checkLength50(ojb){
	var value = ojb.value;
	if (value.length > 50) {
		alert("จำนวนตัวอักษรมีจำนวน 50 ตัวอักษร กรุณาตรวจสอบ");
		ojb.value = "";
		ojb.focus();
	}
}